package com.weiyi.chat.presenter;

import android.app.Activity;

import com.google.gson.Gson;
import com.weiyi.chat.api.IResetPsw;
import com.weiyi.chat.api.IUserList;
import com.weiyi.chat.common.MyStringCallBack;
import com.weiyi.chat.domain.UserList;
import com.weiyi.chat.ui.activity.ResetPswActivity;
import com.weiyi.chat.ui.fragment.MessageFragment;
import com.weiyi.chat.utils.MyReusouce;
import com.zhy.http.okhttp.OkHttpUtils;

import okhttp3.Request;

/**
 * Created by Lee on 2017/8/24 0024.
 */

public class UserListPresenter implements IUserList.presenter {


    private final Activity Context;
    private MessageFragment mView;

    public UserListPresenter(Activity activity, MessageFragment messageFragment) {
        this.Context = activity;
        mView = messageFragment;
    }

    @Override
    public void requestAdminList() {
        OkHttpUtils.post()
                .url(MyReusouce.ADMIN_LIST_URL)
                .build()
                .execute(new MyStringCallBack(Context) {
                    @Override
                    public void onBefore(Request request, int id) {

                    }

                    @Override
                    public void onAfter(int id) {

                    }

                    @Override
                    public void onRequestSucess(String response) {
                        Gson gson = new Gson();
                        UserList userList = gson.fromJson(response, UserList.class);
                        mView.onResetAdminSucess(userList.getBody());
                    }

                    @Override
                    public void onRequstFail(String errorInfo) {
                        mView.showError(errorInfo);
                    }
                });
    }

    @Override
    public void requestAllUserList() {
        OkHttpUtils.post()
                .url(MyReusouce.USER_LIST_URL)
                .addParams("submit", "1")
                .build()
                .execute(new MyStringCallBack(Context) {
                    @Override
                    public void onBefore(Request request, int id) {

                    }

                    @Override
                    public void onAfter(int id) {

                    }

                    @Override
                    public void onRequestSucess(String response) {
                        Gson gson = new Gson();
                        UserList userList = gson.fromJson(response, UserList.class);
                        mView.onResetUserSucess(userList.getBody());
                    }

                    @Override
                    public void onRequstFail(String errorInfo) {
                        mView.showError(errorInfo);
                    }
                });
    }

    @Override
    public void loadingDatasFromCache() {

    }
}
